extends Area2D

@export var speed = 400
var screen_size
var origin_width
var width

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	screen_size = get_viewport_rect().size
	origin_width = $Sprite2D.get_rect().size.x
	
	reset()


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if Input.is_action_pressed("move_right"):
		position.x += speed * delta
	if Input.is_action_pressed("move_left"):
		position.x -= speed * delta
	
	var scaled_half_width = get_scaled_width() / 2
	if position.x + scaled_half_width > screen_size.x:
		position.x = screen_size.x - scaled_half_width
	elif position.x - scaled_half_width < 0:
		position.x = scaled_half_width

func reset() -> void:
	width = screen_size.x / 6
	scale.x = width / origin_width
	
	position = Vector2(screen_size.x/2, screen_size.y - 10)

func make_larger() -> void:
	scale.x += 0.2
	if scale.x > 2.0:
		scale.x = 2.0
	get_parent().get_node("HUD").show_message("Paddle Larger")
	print("item function: larger paddle to ", scale.x)
	
func make_smaller() -> void:
	scale.x -= 0.2
	if scale.x < 0.5:
		scale.x = 0.5
	get_parent().get_node("HUD").show_message("Paddle Smaller")
	print("item function: smaller paddle to ", scale.x)
		
func get_scaled_width() -> float:
	return width * scale.x
	
func make_ball_speedup() -> void:
	print("item function: ball speedup")
	get_parent().get_node("HUD").show_message("Ball Speedup")
	var ball = get_parent().get_node("Ball")
	ball.speedup()
	
func make_ball_slowdown() -> void:
	print("item function: ball slowdown")
	get_parent().get_node("HUD").show_message("Ball Slowdown")
	var ball = get_parent().get_node("Ball")
	ball.slowdown()

func _on_area_entered(area: Area2D) -> void:
	if area.is_in_group("item"):
		if area.get_item_type() == ItemType.ITEM_TYPE.SCALE_UP:
			make_larger()
		elif area.get_item_type() == ItemType.ITEM_TYPE.SCALE_DOWN:
			make_smaller()
		elif area.get_item_type() == ItemType.ITEM_TYPE.BALL_SPEEDUP:
			make_ball_speedup()
		elif area.get_item_type() == ItemType.ITEM_TYPE.BALL_SLOWDOWN:
			make_ball_slowdown()
